# -*- coding: utf-8 -*-
# pref.py
#
# Copyright (C) Marcos Pinto 2008 <markybob@gmail.com>
#
# prion is free software.
# 
# This program is free software; you can redistribute it and/or modify it under 
# the terms of the GNU General Public License as published by the Free Software 
# Foundation; either version 3 of the License, or (at your option) any later 
# version. This program is distributed in the hope that it will be useful, but 
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
# details. You should have received a copy of the GNU General Public License 


import pickle
import os.path

DEFAULT_PREFS = {
    "close_to_tray": True,
    "start_in_tray": False,
    "username": "",
    "team": 0,
    "win": "Windows: 0: 0: 0",
    "lin": "Linux: 0: 0: 0",
    "ppc": "PPC: 0: 0: 0",
    "xint": "XINT: 0: 0: 0",
    "ps3": "PS3: 0: 0: 0",
    "ati": "ATI: 0: 0: 0:",
    "nv": "NV: 0: 0: 0",
    "total": "total: 0: 0: 0",
    "team_last": 0,
    "team_cpu": 0,
    "team_id": 0,
    "team_score": 0,
    "team_wu": 0,
    "team_rank": 0,
    "user_last": 0,
    "user_cpu50": 0,
    "user_cpu7": 0,
    "user_score": 0,
    "user_wu": 0,
    "user_rank": 0,
    "folder":  os.path.join(os.path.expanduser("~"), "folding"),
    "use_proxy": False,
    "proxy_name": "",
    "proxy_passwd": "",
    "proxy_auth": False,
    "proxy_host": "",
    "proxy_port": '3128',
}

class Preferences:
    def __init__(self, filename=None, global_defaults=True, defaults=None):
        self.mapping = {}
        if defaults is not None:
            for key in defaults.keys():
                self.mapping.setdefault(key, defaults[key])
        
        if global_defaults is True:
            self.mapping = DEFAULT_PREFS
                
        self.config_file = filename
        if self.config_file is not None:
            self.load(self.config_file)
        self.change_hooks=[]
    
    def __getitem__(self, key):
        return self.mapping[key]

    def onValueChanged(self,value,method):
        self.change_hooks.append([value,method])
    
    def __setitem__(self, key, value):
        if key not in self.mapping or self.mapping[key]!=value:
            self.mapping[key] = value 
            for hook in self.change_hooks: 
                if (hook[0]==key): hook[1]() 

    def __delitem__(self, key):
        del self.mapping[key]
    
    def __len__(self):
        return len(self.mapping)
    
    def has_key(self, key): return self.mapping.has_key(key)
    def items(self): return self.mapping.items()
    def keys(self): return self.mapping.keys()
    def values(self): return self.mapping.values()
    
    def save(self, filename=None):
        if filename is None:
            filename = self.config_file
        try:
            pkl_file = open(filename, 'wb')
            pickle.dump(self.mapping, pkl_file)
            pkl_file.close()
        except IOError:
            pass

    def load(self, filename=None):
        if filename is None:
            filename = self.config_file
        try:
            pkl_file = open(filename, 'rb')
            self.dump = pickle.load(pkl_file)
            self.mapping.update(self.dump)
            pkl_file.close()
        except IOError:
            pass
        except EOFError:
            pkl_file.close()
            pass
    
    def set(self, key, value):
        self[key] = value

    def get(self, key):
        try:
            value = self.mapping[key]
            return value
        except KeyError:
            return None
    
    def remove(self, key):
        self.mapping.pop(key)
    
    def clear(self):
        self.mapping.clear()
        
    def printout(self):
        for key in self.mapping.keys():
            print key, ':', self.mapping[key]
